package com.example.que;

public class NoneThreadSafeQueue<E> {

    private Node<E> head;
    private Node<E> tail;

    private int size;

    public NoneThreadSafeQueue() {
    }

    public void addLast(E element) {
        Node<E> newNode = new Node<>(element, null);
        if (head == null) {
            head = newNode;
        } else {
            tail.next = newNode;
        }
        tail = newNode;
        size++;
    }

    public E removeFirst() {
        if (head == null) {
            return null;
        }
        E e = head.getE();
        head = head.getNext();
        if (head == null) {
            tail = null;
        }
        size--;

        return e;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public E getIndex(int index) {
        if (isEmpty()) {
            throw new IllegalStateException("The queue is empty");
        }
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
        int currentIndex = 0;
        Node<E> current = head;
        while (currentIndex != index) {
            currentIndex++;
            current = current.getNext();
        }
        return current.getE();
    }

    static class Node<E> {
        E e;
        Node<E> next;

        public Node() {
        }

        public Node(E e, Node<E> next) {
            this.e = e;
            this.next = next;
        }

        public E getE() {
            return e;
        }

        public void setE(E e) {
            this.e = e;
        }

        public Node<E> getNext() {
            return next;
        }

        public void setNext(Node<E> next) {
            this.next = next;
        }
    }

}
